package dev.werillib.camera

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.view.Window
import android.view.WindowManager
import androidx.core.content.ContextCompat
import dev.werillib.R
import dev.werillib.android.click
import dev.werillib.android.clickWithTrigger
import kotlinx.android.synthetic.main.activity_take_photo_dialog.*
import java.io.File

class TakePhotoDialogActivity : TakePhotoActivity() {
    private var outClose: Boolean = true
    override fun onCreate(savedInstanceState: Bundle?) {
        if (outClose) {
            setTheme(R.style.DialogCancelActivityStyle)
        } else {
            setTheme(R.style.DialogActivityStyle)
        }

        val window: Window = window
        window.requestFeature(Window.FEATURE_NO_TITLE)
        window.setGravity(Gravity.BOTTOM)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_take_photo_dialog)

        window.setLayout(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.WRAP_CONTENT
        )

        way1.clickWithTrigger {
            takePhoto.onPickFromGallery()
        }

        way2.clickWithTrigger {
            takePhoto.onPickFromCapture()
        }

        cancel.clickWithTrigger {
            finish()
        }
    }

    fun setCancelOnTouchOutSide(outClose: Boolean): Activity {
        this.outClose = outClose
        return this
    }

    val PATH: String = "path"

    override fun takeSuccess(file: File) {
        super.takeSuccess(file)
        val intent = Intent()
        intent.putExtra(PATH, file.path)
        setResult(Activity.RESULT_OK, intent)
        finish()
    }

    override fun takeFail() {
        super.takeFail()
        finish()
    }
}
